﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WZ.Stock.Data;

namespace WZ.WebSpider
{
    public class dic_CompanyClassSpider : IWebSpider<Entity.dic_CompanyClass>
    {

        public string Code
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Url
        {
            get { return "http://money.finance.sina.com.cn/q/view/newFLJK.php?param={0}"; }
        }

        public Encoding ContextEncoding
        {
            get { return Encoding.Default; }
        }

        public System.Text.RegularExpressions.Regex regex
        {
            get { return new System.Text.RegularExpressions.Regex(@"\""(?<Code>(\w+))\,(?<Title>[^,]*)"); }
        }

        public List<Entity.dic_CompanyClass> GetWebData(string urlParam)
        {
            WZ.Utils.WebHttp.HttpClient client = new Utils.WebHttp.HttpClient(this.ContextEncoding);
            string url = string.Format(this.Url, urlParam);
            string webstr = client.GetString(url);
            MatchCollection matchs = regex.Matches(webstr);
            List<Entity.dic_CompanyClass> resultT = new List<Entity.dic_CompanyClass>();
            for (int i = 0; i < matchs.Count; i++)
            {
                Entity.dic_CompanyClass item = WZ.WebSpider.Serialize.ConverModel.FillModel<Entity.dic_CompanyClass>(matchs[i].Groups);
                item.TypeName = urlParam;
                item.CompanyUrlSina = "http://vip.stock.finance.sina.com.cn/mkt/#" + item.Code;
                resultT.Add(item);
            }

            return resultT;
        }

        public bool Save(Entity.dic_CompanyClass t)
        {
            dic_CompanyClassDAL dal = new dic_CompanyClassDAL();
            return dal.Insert(t) > 0;
        }

        public int DepIndex
        {
            get { throw new NotImplementedException(); }
        }
    }
}
